﻿using CSRedis;
using System;
using System.Collections.Generic;
using TYG.Models;

namespace TYG.RedisRepository
{

    public class RedisInstance
    {
        
        private static RedisInstance instance;
        public static RedisInstance Instance {
            get {
                if (instance==null)
                {
                    instance = new RedisInstance();
                }
                return instance;
            }
        }
        public CSRedisClient DefaultRedisClient = new CSRedis.CSRedisClient("127.0.0.1:6379,defaultDatabase=13,poolsize=50,ssl=false,writeBuffer=10240,prefix=gm_run_");

        public  void SingleSave<T>(T channel)
        {
            RedisHelper<T>.Initialization(DefaultRedisClient);
          //  RedisHelper.StartPipe(p => p.Set(typeof(Channel).Name,channel));
            RedisHelper<T>.StartPipe(a=>a.GetSet(typeof(T).Name, channel)) ;
        }

        public void MultiSave<T>(T channel) {
            RedisHelper<T>.Initialization(DefaultRedisClient);
            RedisHelper<T>.StartPipe(a => a.Append(typeof(T).Name, channel));
        }
        public IEnumerable<T> MultiGet<T>() {
            RedisHelper<T>.Initialization(DefaultRedisClient);
            var data = RedisHelper<T>.StartPipe(a => a.Get(typeof(T).Name));
         return  Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>>(data.ToString());
        }

        public T SingleGet<T>()
        {
            RedisHelper<T>.Initialization(DefaultRedisClient);
           return  RedisHelper<T>.Get<T>(typeof(T).Name);
           
        }
    }
}
